package com.sfh.cpc.user;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.sfh.cpc.com.entity.EUser;
import com.sfh.cpc.com.entity.RspBase;
import com.sfh.cpc.com.req.ReqUserLogin;
import com.sfh.cpc.com.widget.WidgetBaseToolbar;
import com.sfh.cpc.user.model.UserModel;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;
import com.sfh.lib.ui.AbstractLifecycleActivity;
import com.sfh.cpc.R;

/**
 * 登录
 */
public class LoginActivity extends AbstractLifecycleActivity<UserModel> {

    public static void startUI(Context context) {

        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    private EditText mEmailView;
    private EditText mPasswordView;
    private WidgetBaseToolbar toolbar;

    @RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_login_activity);
        toolbar  = findViewById(R.id.toolbar);
        mEmailView = findViewById(R.id.email);
        toolbar.setTitle("登录").setTextColor(Color.BLACK);
        mPasswordView = findViewById(R.id.password);
        mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
                if (id == EditorInfo.IME_ACTION_DONE || id == EditorInfo.IME_NULL) {
                    attemptLogin();
                    return true;
                }
                return false;
            }
        });

      findViewById(R.id.tv_login).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                attemptLogin();
            }
        });
        findViewById(R.id.tv_zc).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                UserRegisterActivity.startUI(LoginActivity.this);
                finish();
            }
        });

    }


    /**
     * Attempts to sign in or register the account specified by the login form.
     * If there are form errors (invalid email, missing fields, etc.), the
     * errors are presented and no actual login attempt is made.
     */
    private void attemptLogin() {

        mEmailView.setError(null);
        mPasswordView.setError(null);

        String email = mEmailView.getText().toString();
        String password = mPasswordView.getText().toString();

        boolean cancel = false;
        View focusView = null;

        if (TextUtils.isEmpty(email)) {
            mEmailView.setError("账号不能为空");
            focusView = mEmailView;
            cancel = true;
        }
        if (TextUtils.isEmpty(password)) {
            mPasswordView.setError("密码不能为空");
            focusView = mPasswordView;
            cancel = true;
        }
        if (cancel) {
            focusView.requestFocus();
        } else {

            ReqUserLogin req = new ReqUserLogin();
            req.setName(email);
            req.setPwd(password);
            this.getViewModel().login(req);
        }
    }



    @LiveDataMatch
    public void onLoginSuccess(RspBase<EUser> reqBase) {
        if (reqBase.success()) {
            postEvent(reqBase.getData());
            finish();
        } else {
            mPasswordView.setError(reqBase.getMsg());
            mPasswordView.requestFocus();
        }
    }

}

